<template>
  <div id="airlines" class="card col-12 px-0 mt-5">
    <nav aria-label="breadcrumb" role="navigation">
      <ol class="breadcrumb pb-0">
        <li class="breadcrumb-item ms-1" aria-current="page">基础数据</li>
        <li class="breadcrumb-item active" aria-current="page">航空公司信息</li>
      </ol>
    </nav>
    <template v-if="isSearchMode">
      <div class="card-body">
        <form>
          <div class="row mb-2">
            <input
              type="textfield"
              class="form-control"
              placeholder="两字代码"
              size="8"
              v-model.trim="twoCode0"
            />
          </div>
          <div class="row mb-2">
            <input
              type="textfield"
              class="form-control"
              placeholder="三字代码"
              size="8"
              v-model.trim="threeCode0"
            />
          </div>
          <div class="row mb-2">
            <button
              type="button"
              class="btn btn-primary mb-2"
              @click.stop="search()"
            >
              查找
            </button>
            <button
              type="button"
              class="btn btn-secondary"
              @click.stop="reset()"
            >
              重置
            </button>
          </div>
        </form>
      </div>
    </template>
    <template v-else>
      <div class="card-body">
        <button
          type="button"
          @click.stop="newUser()"
          class="btn btn-success"
          v-if="isAdmin"
        >
          新建
        </button>
        <button
          type="button"
          class="btn btn-primary float-end"
          @click.stop="showSearch()"
        >
          查找
        </button>
      </div>
      <table class="table table-striped table-hover table-sm small">
        <thead>
          <tr>
            <th>两字代码</th>
            <th>三字代码</th>
            <th>中文名</th>
            <th>中文简称</th>
            <th class="d-none d-md-table-cell">英文名</th>
            <th class="d-none d-md-table-cell">英文简称</th>
            <th></th>
          </tr>
        </thead>
        <tbody>
          <tr
            v-for="(info, index) in dataList"
            :key="info.id"
            @click.stop="editAirlines(index)"
          >
            <td>{{ info.twoCode }}</td>
            <td>{{ info.threeCode }}</td>
            <td>{{ info.name }}</td>
            <td>{{ info.nickName }}</td>
            <td class="d-none d-md-table-cell">{{ info.englishName }}</td>
            <td class="d-none d-md-table-cell">{{ info.englishNickName }}</td>
            <td><i class="mdi mdi-arrow-right fa-2"></i></td>
          </tr>
        </tbody>
      </table>
      <nav id="pagination-box" class="float-end">
        <my-pagination
          name="pagination"
          :row-count="sc.rowCount"
          :page-total="sc.pageTotal"
          :page-no="sc.pageNo"
          @next-page="nextPage"
          @prev-page="prevPage"
          @direct-page="directPage"
        ></my-pagination>
      </nav>
    </template>

    <my-modal-airlines ref="airlinesModal"></my-modal-airlines>
  </div>
</template>

<script>
import $ from "jquery";
import { searchAirlines, createAirlines } from "@/api/basic-data.js";
import MyPagination from "@/components/my-pagination_h5.vue";
import MyModalAirlines from "@/components/modal/my-modal-airlines.vue";

export default {
  components: {
    MyPagination,
    MyModalAirlines,
  },
  data() {
    return {
      isSearchMode: false,
      sc: {
        rowCount: 0,
        pageNo: 1,
        pageSize: 10,
        pageTotal: 0,
      },
      threeCode0: "",
      twoCode0: "",

      id: 0,
      threeCode: "",
      twoCode: "",
      name: "",
      englishName: "",
      nickName: "",
      englishNickName: "",

      dataList: [],
      modalTitle: "",
    };
  },
  computed: {
    isAdmin() {
      return this.$store.getters.isAdmin;
    },
    readonly() {
      return this.isAdmin === false;
    },
  },
  mounted: function () {
    this.search();
  },
  methods: {
    showErrMsg: function (msg, msgType) {
      this.$store.dispatch("showAlertMsg", {
        errMsg: msg,
        errMsgType: msgType,
      });
    },
    showSearch: function () {
      this.isSearchMode = true;
    },
    search: function () {
      this.isSearchMode = false;
      const params = {
        "sc.pageNo": this.sc.pageNo,
        "sc.pageSize": this.sc.pageSize,
        threeCode: this.threeCode0,
        twoCode: this.twoCode0,
      };

      searchAirlines(params, (jsonResult) => {
        this.dataList = jsonResult.dataList;
        this.sc = jsonResult.page;
      });
    },
    newUser: function () {
      const params = {
        id: 0,
        threeCode: "",
        twoCode: "",
        name: "",
        englishName: "",
        nickName: "",
        englishNickName: "",
      };

      this.$refs.airlinesModal.modal(params).then((v) => this.save(v));
    },
    editAirlines: function (index) {
      const info = this.dataList[index];

      const params = {
        id: info.id,
        threeCode: info.threeCode,
        twoCode: info.twoCode,
        name: info.name,
        englishName: info.englishName,
        nickName: info.nickName,
        englishNickName: info.englishNickName,
      };

      this.$refs.airlinesModal.modal(params).then((v) => this.save(v));
    },
    save: function (params) {
      createAirlines(params, (jsonResult) => {
        if (jsonResult.status !== "OK") {
          this.showErrMsg(jsonResult.errmsg, "danger");
        } else {
          this.showErrMsg("保存成功");
          this.search();
        }
      });
    },
    prevPage: function () {
      this.sc.pageNo = this.sc.pageNo - 1;
      if (this.sc.pageNo < 1) this.sc.pageNo = 1;
      this.search();
    },
    nextPage: function () {
      this.sc.pageNo = this.sc.pageNo + 1;
      this.search();
    },
    directPage: function (pageNo) {
      this.sc.pageNo = pageNo;
      this.search();
    },
    reset: function () {
      this.threeCode0 = "";
      this.twoCode0 = "";
    },
  },
};
</script>
